<template>
  <rk-dialog width="400"
    height="180"
    :title="title"
    :visible="visible"
    :maskClosable="false"
    :destroyOnClose="true"
    @hide="handleHide">
    <el-form ref="unitUsingRef"
      :model="form"
      :rules="rules"
      @validate="validateForm">
      <el-form-item label="失效时间">
        <el-date-picker v-model="form.endtime"
          placeholder="请选择时间"
          valueFormat="YYYY-MM-DD" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary"
        @click="handleConfirm">{{sunmitTit}}</el-button>
      <el-button @click="hanldeClose">取消</el-button>
    </template>
  </rk-dialog>
</template>
 <script setup>
import { UNIT_STATUS_ENUMS } from '@/enums/enums-system.js'
import { UNIT_STATUS } from '@/enums/enums-unit.js'
import { ref } from 'vue'
import XEUtils from 'xe-utils'
import { useUnitApi } from '@/api/index.js'
import { ElLoading, ElMessage } from 'element-plus'

const { disableOrEnablePubagency } = useUnitApi();
const emit = defineEmits(['confirm'])
const defaultForm = {
  endtime: new Date().format('yyyy-MM-dd'),
}
const unitUsingRef = ref('')
const title = ref('单位')
const sunmitTit = ref('')
const visible = ref(false)
let form = reactive({
  ...defaultForm
}
)
let currentNode = reactive({})
const rules = reactive({
  endtime: [{ required: true, message: '请选择时间', trigger: 'change' }]
})
/** 打开弹出层 */
const handleOpen = ({ seleModel: model }) => {
  visible.value = true
  const { status } = model
  form.endtime = (status === UNIT_STATUS.OPEN) ? new Date().format('yyyy-MM-dd') : '2099-12-31'
  const text = getOpenStopText(model)
  title.value = `${text}单位`
  sunmitTit.value = text
  currentNode = {
    ...model
  }
}
const handleHide = () => {
  form = XEUtils.clone(defaultForm, true)
}
/** 关闭弹窗 */
const hanldeClose = () => {
  visible.value = false
}
const handleConfirm = () => {
  unitUsingRef.value.validate(valid => {
    if (valid) {
      handleRechangeData()
    } else {
      return false;
    }
  })
}
const handleRechangeData = async () => {
  const { guid, status } = currentNode
  const loading = ElLoading.service({ fullscreen: true })
  let params = {
    guids: guid,
    status: (status === UNIT_STATUS.OPEN) ? UNIT_STATUS.STOP : UNIT_STATUS.OPEN,
    endtime: form.endtime
  }
  const result = await disableOrEnablePubagency(params)
  const { data, code, msg } = result
  if (code === 200) {
    loading.close()
    visible.value = false
    console.error(msg)
    emit('confirm')
    ElMessage({
      message: `${msg}！`,
      type: 'success',
    })
  } else {
    loading.close()
    console.error(msg)
  }
}
/** 重置表单 */
const resetForm = () => {
  unitUsingRef.value.resetFields()
  unitUsingRef.value.clearValidate()
  // 重新赋值
  form = XEUtils.clone(defaultForm, true)
}
/** 表单校验 */
const validateForm = () => {
  let resValid = false
  unitUsingRef.value.validate((valid, failCol) => {
    resValid = valid
  })
  return resValid
}
/** 获取启用停用文字描述 */
const getOpenStopText = ({ status }) => {
  let result = (status === UNIT_STATUS.OPEN)
    ? UNIT_STATUS_ENUMS.options[UNIT_STATUS.STOP].label
    : UNIT_STATUS_ENUMS.options[UNIT_STATUS.OPEN].label
  return result
}

// 对外暴露方法
defineExpose({
  handleOpen
})
  </script>
  <style lang="scss" scoped>
</style>
  
  